const catchError = async (ctx, next) => {
  try {
    console.log(next);
    await next();
  } catch (err) {
    console.log(err);
    if (err.name === "ValidationError") {
      // mongoose 数据验证错误
      ctx.body = {
        code: 400,
        msg: err.message,
      };
    } else if (err.status == 401) {
      // 鉴权失败
      ctx.status = err.status;
      ctx.body = {
        code: 401,
        msg: "用户未登录，请登录后进行访问",
      };
    } else {
      ctx.status = 500;
      ctx.body = {
        code: 500,
        msg: "服务器错误，请稍后重试",
      };
    }
  }
}

module.exports = {
  catchError
}